package uns.fit.gis.ui;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.i18n.LocaleManager;

public class HelpForm extends Form implements CommandListener {
	private Command backCommand;
	private StringItem helpContent;
	
	public HelpForm() {
		super(LocaleManager.getMessage("help"));
		
		backCommand = new Command(LocaleManager.getMessage("back"), Command.BACK, 1);
		addCommand(backCommand);
		
		setCommandListener(this);
		loadHelpContent();
	}

	public void commandAction(Command command, Displayable displayable) {
		if(command == backCommand) {
			MapDrawer.getMidlet().showMapCanvas();
		}
	}
	
	public void loadHelpContent() {
		InputStreamReader in = new InputStreamReader(this.getClass().getResourceAsStream(
						"/localization/help_"
						+ MapDrawer.getMidlet().getSettings().getLocale()
						+ ".properties"));
		helpContent = new StringItem("", "");
		StringBuffer strBuf = new StringBuffer();
		try {
			int chars;
		    while ((chars = in.read()) != -1){
		    	strBuf.append((char) chars);
		    }
		    helpContent.setText(LocaleManager._convertString(strBuf.toString()));
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		append(helpContent);
	}
}
